package com.lemon.smartsafety.ui.activity.address;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.entity.event.address.AddressBookSystemFragmentShowErrorEvent;
import com.lemon.smartsafety.entity.tab.TabEntity;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.ui.fragment.address.AddressBookCommonFragment;
import com.lemon.smartsafety.ui.fragment.address.AddressBookListFragment;
import com.lemon.smartsafety.ui.fragment.address.AddressBookSystemFragment;
import com.lemon.smartsafety.ui.widget.ViewPagerEnableScroll;
import com.lemon.smartsafety.utils.common.LoggerHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;

/**
 * Created by Cooliean on 16/4/11.
 */
public class    AddressBookMainActivity extends SwipeBackActivity {
    @Bind(R.id.tl_com)
    CommonTabLayout       mTabs;
    @Bind(R.id.viewPager)
    ViewPagerEnableScroll mViewPager;


    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    FragmentStatePagerAdapter mViewPagerAdapter;
    //private String[] mTitles = {"通讯录", "共享通讯录", "系统通讯录"};
    private String[] mTitles = {"通讯录", "共享通讯录"};

    private Map<String, Fragment> fragmentMap = new HashMap<>();


    @Override
    protected void initTitle() {
        setBarTitle("通讯录");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(AddressBookMainActivity.class);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            callFinish();
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected int getContentViewId() {
        return R.layout.activity_address_book_main;
    }


    @Override
    protected void initViews() {
        super.initViews();
        initViewPager();
        //        mTabs.showMsg(1, 10);
        //mTabs.setMsgMargin(0, -5, 30);
    }


    private void initViewPager() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }

        mViewPagerAdapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                if (position == 0) {
                    return AddressBookListFragment.newInstance();
                } else if (position == 1) {
                    return AddressBookCommonFragment.newInstance();
                    //return AddressBookInviteFragment.newInstance();
                }
                return AddressBookSystemFragment.newInstance();
                //return AddressBookApplyFragment.newInstance();
            }

            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles[position];
            }
        };
        mViewPager.setAdapter(mViewPagerAdapter);
        //mViewPager.setScrollable(false);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if(position==2){
                    AppService.getBus().post(new AddressBookSystemFragmentShowErrorEvent(false));
                }else{
                    AppService.getBus().post(new AddressBookSystemFragmentShowErrorEvent(true));
                }
                mTabs.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mTabs.setTabData(mTabEntities);
        mTabs.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
//                if (position == 0) {
//                    mTabs.showMsg(0, new Random().nextInt(100) + 1);
//                }
            }
        });
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
     /*   super.onCreateOptionsMenu(menu);
        boolean menuResult = super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_history, menu);
        return menuResult;
*/
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_search) {
            //搜索
            LoggerHelper.error(getClass(),"搜索");
            startActivity(new Intent(this,SearchBookActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }




}
